home *** CD-ROM | disk | FTP | other *** search
/ Computer Select (Limited Edition) / Computer Select.iso / dobbs / v17n02 / 3d.exe / L3.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-22  |  2.6 KB  |  56 lines

  1. /* Draws all visible faces (faces pointing toward the viewer) in the
  2.    specified object.  The object must have previously been transformed
  3.    and projected, so that the ScreenVertexList array is filled in. */
  4. #include "polygon.h"
  5.  
  6. void DrawVisibleFaces(struct Object * ObjectToXform)
  7. {
  8.    int i, j, NumFaces = ObjectToXform->NumFaces, NumVertices;
  9.    int * VertNumsPtr;
  10.    struct Face * FacePtr = ObjectToXform->FaceList;
  11.    struct Point * ScreenPoints = ObjectToXform->ScreenVertexList;
  12.    long v1,v2,w1,w2;
  13.    struct Point Vertices[MAX_POLY_LENGTH];
  14.    struct PointListHeader Polygon;
  15.  
  16.    /* Draw each visible face (polygon) of the object in turn */
  17.    for (i=0; i<NumFaces; i++, FacePtr++) {
  18.       NumVertices = FacePtr->NumVerts;
  19.       /* Copy over the face's vertices from the vertex list */
  20.       for (j=0, VertNumsPtr=FacePtr->VertNums; j<NumVertices; j++)
  21.          Vertices[j] = ScreenPoints[*VertNumsPtr++];
  22.       /* Draw only if outside face showing (if the normal to the
  23.          polygon points toward the viewer; that is, has a positive
  24.          Z component) */
  25.       v1 = Vertices[1].X - Vertices[0].X;
  26.       w1 = Vertices[NumVertices-1].X - Vertices[0].X;
  27.       v2 = Vertices[1].Y - Vertices[0].Y;
  28.       w2 = Vertices[NumVertices-1].Y - Vertices[0].Y;
  29.       if ((v1*w2 - v2*w1) > 0) {
  30.          /* It is facing the screen, so draw */
  31.          /* Appropriately adjust the extent of the rectangle used to
  32.             erase this page later */
  33.          for (j=0; j<NumVertices; j++) {
  34.             if (Vertices[j].X > EraseRect[NonDisplayedPage].Right)
  35.                if (Vertices[j].X < SCREEN_WIDTH)
  36.                   EraseRect[NonDisplayedPage].Right = Vertices[j].X;
  37.                else EraseRect[NonDisplayedPage].Right = SCREEN_WIDTH;
  38.             if (Vertices[j].Y > EraseRect[NonDisplayedPage].Bottom)
  39.                if (Vertices[j].Y < SCREEN_HEIGHT)
  40.                   EraseRect[NonDisplayedPage].Bottom = Vertices[j].Y;
  41.                else EraseRect[NonDisplayedPage].Bottom=SCREEN_HEIGHT;
  42.             if (Vertices[j].X < EraseRect[NonDisplayedPage].Left)
  43.                if (Vertices[j].X > 0)
  44.                   EraseRect[NonDisplayedPage].Left = Vertices[j].X;
  45.                else EraseRect[NonDisplayedPage].Left = 0;
  46.             if (Vertices[j].Y < EraseRect[NonDisplayedPage].Top)
  47.                if (Vertices[j].Y > 0)
  48.                   EraseRect[NonDisplayedPage].Top = Vertices[j].Y;
  49.                else EraseRect[NonDisplayedPage].Top = 0;
  50.          }
  51.          /* Draw the polygon */
  52.          DRAW_POLYGON(Vertices, NumVertices, FacePtr->Color, 0, 0);
  53.       }
  54.    }
  55. }
  56.